package com.detu.ivos.geofence.service.impl;

import com.detu.ivos.base.exception.ServiceException;
import com.detu.ivos.base.response.StatusCode;
import com.detu.ivos.geofence.mapper.GeofenceMapper;
import com.detu.ivos.geofence.pojo.dto.GeofenceQuery;
import com.detu.ivos.geofence.pojo.dto.GeofenceSaveParam;
import com.detu.ivos.geofence.pojo.entity.Geofence;
import com.detu.ivos.geofence.pojo.vo.GeofenceVO;
import com.detu.ivos.geofence.service.GeofenceService;
import com.detu.ivos.vehicle.mapper.VehicleMapper;
import com.detu.ivos.vehicle.pojo.dto.VehicleQuery;
import com.detu.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {

    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    //查询围栏列表
    @Override
    public List<GeofenceVO> selectGeo(GeofenceQuery geofenceQuery) {
        List<GeofenceVO> geofenceVOS = geofenceMapper.selectGeo(geofenceQuery);
        if (geofenceVOS == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        //遍历每一个围栏
        for (int i = 0; i < geofenceVOS.size(); i++) {
            //获取每一个围栏对象
            GeofenceVO geofenceVO = geofenceVOS.get(i);
            //封装车辆查询对象
            VehicleQuery vehicleQuery=new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());//围栏id

            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            //遍历车辆集合,设置车辆总数
            geofenceVO.setTotalNum(vehicleVOS.size());
            //设置可用车辆数量
            int count=0;
            for (VehicleVO vehicleVO: vehicleVOS){
                if (vehicleVO.getStatus().equals("1")){
                    count++;
                }
            }
            geofenceVO.setAvailableNum(count);
            geofenceVO.setVehicleList(vehicleVOS);
        }


        return geofenceVOS;
    }

    //添加围栏
    @Override
    public void saveGeofence(GeofenceSaveParam geofenceSaveParam) {
        Geofence geofence = new Geofence();
        //复制位置信息和名字
        BeanUtils.copyProperties(geofenceSaveParam, geofence);
        if (geofence.getId() == null) {
            geofence.setCreateTime(new Date());
            geofence.setStatus("1");
            log.debug("创建一个围栏：" + geofence);
            geofenceMapper.insetGeofence(geofence);
            log.debug("创建围栏成功");
        }
//        else {
//
//           geofence.setUpdateTime(new Date());
//            log.debug("更新围栏的");
//           geofenceMapper.updateGeofence(geofence);
//      }
    }

    @Override
    public void updateStatus(Long geoId, String geoStatus) {
        Geofence geofence = new Geofence();
        geofence.setUpdateTime(new Date());
        geofence.setStatus(geoStatus);
        geofence.setId(geoId);
        log.debug("将把这个对象映射到数据库：" + geofence);
        int i = geofenceMapper.updateGeofence(geofence);
        log.debug("状态更新了{}条：", i);
        if (i <= 0) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }


    @Override
    public void deleteById(Long id) {
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setId(id);
        List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
        if (vehicleVOS.size() > 0 && vehicleVOS != null) {
            throw new ServiceException(StatusCode.VALIDATE_EXISTS);
        } else {
            int i = geofenceMapper.deleteById(id);
            if (i <= 0) {
                throw new ServiceException(StatusCode.OPERATION_FAILED);
            }
        }


    }
}
